package com.itcast.jdbc.test03;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JdbcTest02 {

    public static void main(String[] args) throws SQLException {

        //练习插入操作
        Brand brand=new Brand(null,"苹果手机","sun公司",1,"苹果为爱而生",1);


        int i = saveBrand(brand);



    }

    private static int saveBrand(Brand brand) throws SQLException {
        //获取连接
        Connection connection = DatasourceUtils.getConnection();

        //编写sql
        String sql="insert into tb_brand values(null,?,?,?,?,?)";
        //绑定preparedStatement的参数
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1,brand.getBrandName());
        preparedStatement.setString(2,brand.getCompanyName());
        preparedStatement.setInt(3,brand.getOrdered());
        preparedStatement.setString(4,brand.getDescription());
        preparedStatement.setInt(5,brand.getStatus());

        //执行
        int i = preparedStatement.executeUpdate();

        //关闭资源
        preparedStatement.close();
        //归还连接给连接池
        connection.close();

        return i;

    }





}